package com.zzyl.serve.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author qyy
 * @date 2025-08-14
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlan nursingPlan = getById(id);
        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.listByPlanId(id);
        //封装成dto
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param npd 护理计划（包括护理项目）
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto npd)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(npd,nursingPlan);
        save(nursingPlan);
        List<NursingProjectPlan> projectPlans = npd.getProjectPlans();
        if(projectPlans!=null && !projectPlans.isEmpty()){
            nursingProjectPlanMapper.insertBatch(nursingPlan.getId(),projectPlans);
        }
        return 1;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //更新
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        updateById(nursingPlan) ;
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(projectPlans!=null && !projectPlans.isEmpty()){
            //删除原来的nursing_project_plan表格中内容
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            //插入新内容
            projectPlans.forEach(projectPlan->{projectPlan.setUpdateTime(new Date());});
            nursingProjectPlanMapper.insertBatch(nursingPlan.getId(),projectPlans);
        }


         return 1;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))? 1:0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanById(Long id)
    {
        removeById(id);
        nursingProjectPlanMapper.deleteByPlanId(id);
        return 1;
    }

    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlan> listAll() {
        List<NursingPlan> list = lambdaQuery().eq(NursingPlan::getStatus, 1).list();
        return list;
    }
}
